package com.wsoft.hr.pm.controller;

import cn.hutool.core.collection.CollUtil;
import com.wsoft.hr.pm.enums.HrProcessFlowStageEnum;
import com.wsoft.hr.pm.query.process.template.HrPmProcessTemplateQuery;
import com.wsoft.hr.pm.query.process.template.HrPmProcessTemplateSaveQuery;
import com.wsoft.hr.pm.query.process.template.HrProcessTemplateCardQuery;
import com.wsoft.hr.pm.service.IHrPmProcessTemplateService;
import com.wsoft.hr.pm.vo.process.template.HrPmProcessTemplateDataVO;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * 模版考核流程 前端控制器
 *
 * @author zhonglj
 * @since 2025年02月28日 18:18:07
 */
@RestController
@Tag(name = "考评模版-考核流程")
@RequestMapping("/hr/pm/process/template")
public class HrPmProcessTemplateController {
    @Resource
    private IHrPmProcessTemplateService processTemplateService;

    @Operation(summary = "获取考评模版考核流程数据", description = "根据考评模版id获取模版考核流程数据")
    @GetMapping("/getByTemplateId")
    public ResponseResult<HrPmProcessTemplateDataVO> getByTemplateId(@RequestParam("templateId") Long templateId) {
        return ResponseResult.success(processTemplateService.getByTemplateId(templateId));
    }

    @Operation(summary = "暂时保存考评模版考核流程", description = "暂时保存考评模版考核流程")
    @PostMapping("/tempSave")
    public ResponseResult tempSave(@RequestBody HrPmProcessTemplateSaveQuery saveQuery) {
        // 1. 排序流程阶段
        saveQuery.getFlowStages().sort(Comparator.comparingInt(HrPmProcessTemplateQuery::getSort));
        // 2. 对每个流程阶段的卡片排序
        saveQuery.getFlowStages().forEach(flowStage -> {
            flowStage.getCards().sort(
                    Comparator.comparingInt(HrProcessTemplateCardQuery::getSerialSort)
                            .thenComparingInt(HrProcessTemplateCardQuery::getParallelSort)
            );
        });
        // 设置默认的工作流程
        setDefaultProcessDefKey(saveQuery);
        processTemplateService.saveProcess(saveQuery);
        return ResponseResult.success("[sys.msg.success]");
    }


    @Operation(summary = "保存考评模版考核流程", description = "保存考评模版考核流程")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Validated HrPmProcessTemplateSaveQuery saveQuery) {
        // 1. 排序流程阶段
        saveQuery.getFlowStages().sort(Comparator.comparingInt(HrPmProcessTemplateQuery::getSort));
        // 2. 对每个流程阶段的卡片排序
        saveQuery.getFlowStages().forEach(flowStage -> {
            flowStage.getCards().sort(
                    Comparator.comparingInt(HrProcessTemplateCardQuery::getSerialSort)
                            .thenComparingInt(HrProcessTemplateCardQuery::getParallelSort)
            );
        });
        // 设置默认的工作流程
        setDefaultProcessDefKey(saveQuery);
        processTemplateService.saveProcess(saveQuery);
        return ResponseResult.success("[sys.msg.success]");
    }

    private void setDefaultProcessDefKey(HrPmProcessTemplateSaveQuery saveQuery) {
        if (saveQuery != null) {
            List<HrPmProcessTemplateQuery> flowStages = saveQuery.getFlowStages();
            if (CollUtil.isNotEmpty(flowStages)) {
                for (HrPmProcessTemplateQuery flowStage : flowStages) {
                    HrProcessFlowStageEnum stageEnum = HrProcessFlowStageEnum.getEnum(flowStage.getFlowStage());
                    if (stageEnum != null) {
                        flowStage.setProcDefKey(stageEnum.getProcessDefKey());
                    }
                }
            }
        }
    }
}
